package service.user;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import service.CommonServlet;

public class GetUserHead extends CommonServlet {

	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		try
		{
			int h_uNo;
			String uNoString = request.getParameter("uNo");
			if(uNoString==null) h_uNo = uNo;
			else h_uNo = Integer.parseInt(uNoString);
			
			String sql="select * from head where u_no = ?";
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setInt(1, h_uNo);
			ResultSet rs = pstmt.executeQuery();
			String photoPath = "";
			if(rs.next() && rs.getString("h_data")!=null )
			{
				String h_data = rs.getString("h_data");
				photoPath = mPhotoRootPath + h_uNo + File.separator + "head" + File.separator + h_data;
			
			}
			else
			{
				photoPath =  mPhotoRootPath + "default.jpg";
			}
				
			OutputStream os = response.getOutputStream();
			FileInputStream fis = new FileInputStream(photoPath);
			byte[] buffer = new byte[8192];
			int n = 0;
			while ( (n = fis.read(buffer)) > 0 )
			{
				os.write(buffer, 0, n);
			}	
			
			
//			OutputStream os = response.getOutputStream();
//			FileInputStream fis = new FileInputStream("C:\\exp5\\head1.gif");
//			byte[] buffer = new byte[8192];
//			int n = 0;
//			while ( (n = fis.read(buffer)) > 0 )
//			{
//				os.write(buffer, 0, n);
//			}
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
			//response.getWriter().println(e.getMessage());
		}
	}

	/**
	 * Constructor of the object.
	 */
	public GetUserHead() {
		super();
	}

}
